package com.qianyitian.hope2.analyzer.analyzer;

import com.qianyitian.hope2.analyzer.model.KLineInfo;
import com.qianyitian.hope2.analyzer.model.ResultInfo;
import com.qianyitian.hope2.analyzer.model.Stock;
import com.qianyitian.hope2.analyzer.util.AlgorithmUtil;
import com.qianyitian.hope2.analyzer.util.Utils;

import java.util.List;

public class FiveFiveAnalyzer extends AbstractStockAnalyzer {


    // consider how many days before now.
    int daysToNow = 4;

    //5日均线
    int n = 5;

    public FiveFiveAnalyzer() {
    }

    @Override
    public boolean analyze(ResultInfo resultInfo, Stock stock) {
        boolean ok = false;
        List<KLineInfo> infos = stock.getkLineInfos();
        if (infos.size() < daysToNow + n) {
            // 元数据天数要求大于考察天数
            return ok;
        }

        double[] doubles = AlgorithmUtil.maN(infos, 5, 5);

        for (int i = infos.size() - daysToNow, j = 0; i < infos.size(); i++, j++) {
            if (infos.get(i).getClose() < doubles[j]) {
                return false;
            }
        }

        KLineInfo current = infos.get(infos.size() - 1);
        double ma5 = doubles[doubles.length - 1];

        if (current.getLow() > ma5) {
            return false;
        }
        float turnover = getTurnover(stock);
        if (stock.isA() && turnover < 2) {
            return false;
        }
        resultInfo.setValue(getTurnover(stock));
        return true;
    }

    public String format(Stock stock, KLineInfo current, KLineInfo check) {
        StringBuilder sb = new StringBuilder();
        return (sb.toString());
    }

    @Override
    public String getDescription() {
        return daysToNow + "天之内股价在5日线之上\n\r";
    }

    public int getDaysToNow() {
        return daysToNow;
    }

    public void setDaysToNow(int daysToNow) {
        this.daysToNow = daysToNow;
    }

    public int getWeight() {
        return 100;
    }

}
